package com.gxdf.extendedcontrols.gxdfclient.components;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import com.artech.R;
import com.gxdf.extendedcontrols.gxdfclient.data.Field;
import com.gxdf.extendedcontrols.gxdfclient.data.RenderingType;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GxdfGroupView extends GxdfField implements OnClickListener {	
	
	final static int MAX_CHILD = 10;
	TextView title;
	ImageButton addButton;	
	Context context;
	List<View> childs;
	boolean isValid;
	int childcount;

	public GxdfGroupView(Context context, Field f) {
		super(context, f);

		this.setOrientation(LinearLayout.VERTICAL);
		this.setPadding(10, 20, 10, 20);
		this.context = context;
		this.childs = new ArrayList<View>();
		title = new TextView(context);
		title.setText(getField().getName());
		title.setGravity(Gravity.CENTER);

		
		addButton = new ImageButton(context);
		
		addButton.getBackground().setAlpha(0);
		addButton.setBackgroundResource(R.drawable.gx_action_insert_dark);		
		addButton.setId(701);
		addButton.setLayoutParams(new LayoutParams(24, 24));
		LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams)addButton.getLayoutParams();    
		ll.gravity = Gravity.CENTER;
		addButton.setLayoutParams(ll);
		
		//addButton.setText("Agregar " + getField().getName());
		addButton.setOnClickListener(this);
		loadChilds();

	}
	
	
	@Override
	public void drawField() {

		addView(title);
		for (View v : childs) {
			addView(v);
		}
		this.addView(addButton);
	}

	
	private RenderingType getRenderingType(){
		return getField().getType().getRender();
	}


	@Override
	public String getValue() {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void loadChilds() {

		for (String s : field.getValues()) {

			RenderingType render = getRenderingType();

			switch (render) {
			case TEXTBOX_LIST:

				EditText e = new EditText(getContext());
				e.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
						LayoutParams.WRAP_CONTENT));
				e.setText(s);

				GxdfRemovableChild child = new GxdfRemovableChild(
						GxdfGroupView.this.context, e, childs.size(), s, this);
				childs.add(child);

				break;

			default:
				break;
			}

		}
	}
	
	public void saveValues() {

		List<String> newvalues = new ArrayList<String>();

		for (View c : childs) {
			String value = ((GxdfRemovableChild) c).getValue();
			if (!value.isEmpty())
				newvalues.add(value);
		}
		actualValues.clear();
		actualValues.addAll(newvalues);
		newvalues = null;
	}
	
	public boolean isModified(){
		return actualValues.equals(getField().getValues());
	}
	
	public List<String> getActualValues(){
		return this.actualValues;
	}


	@Override
	public boolean errorFormat() {
		boolean invalid = false;

		if (getField().getFormatValidation().isEmpty()) {
			invalid = false;
		} else {
			// TODO: Ejecutar regexp. Si falla return true else return false
			pattern = Pattern.compile(field.getFormatValidation());

			int i = 0;
			while (i < actualValues.size() && !invalid) {
				matcher = pattern.matcher(actualValues.get(i));
				if (!matcher.matches()) {
					invalid = true;
				}
				i++;
			}

		}

		return invalid;
	}


	@Override
	public boolean errorLength() {
		// TODO Auto-generated method stub
		return false;
	}


	@Override
	public boolean isFieldvalid() {
		return isValid;
	}
	
		
/**
 * Delete child view
 */
	@Override
	public void onClick(View v) {

		if (v.getId() == 701) {

			if (childs.size() < MAX_CHILD) {

				RenderingType render = getRenderingType();
				Log.i("ClickView", String.valueOf(v.getId()));

				switch (render) {
				case CALENDAR:
					break;
				default:
					removeView(addButton);
					EditText e = new EditText(getContext());
					e.setLayoutParams(new LayoutParams(
							LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
					GxdfRemovableChild child = new GxdfRemovableChild(
							GxdfGroupView.this.context, e, childs.size(), "",
							GxdfGroupView.this);
					childs.add(child);
					addView(child);
					addView(addButton);
					break;
				}
			} else {
				v.setEnabled(false);
			}

		} else {

			if (!addButton.isEnabled())
				addButton.setEnabled(true);

			GxdfRemovableChild parent = (GxdfRemovableChild) v.getParent();

			removeView(parent);
			childs.remove(childs.get(parent.getIndex()));

		}

	}

}
